# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

sh("bundle install")

default_platform(:android)

desc "Create testing release"
lane :testing do
    gradle(task: "clean assembleCi")
end

desc "Create and deploy production release"
lane :production do
    prompt(text: "Did you write whats new?")

    version_code = get_properties_value(key: "VERSION_CODE", path: "./app/version.properties")
    version_name = get_properties_value(key: "VERSION_NAME", path: "./app/version.properties")

#   Don't create changelog for f-droid because not committing it
#   File.write("metadata/android/en-US/changelogs/" + version_code + ".txt", whats_new)

    # Check Rust formatting and run tests
    Dir.chdir("../evdev/src/main/rust/evdev_manager") do
      sh("cargo fmt --check")
      sh("cargo test --package evdev_manager_core")
    end

    Dir.chdir("..") do
      gradle(task: "testDebugUnitTest")
    end

    github_token = prompt(
                    text: "Github token: ",
                    secure_text: true
                   )

    ENV["KEYSTORE_PASSWORD"] = prompt(
                    text: "Key store password: ",
                    secure_text: true
                   )

    ENV["KEY_PASSWORD"] = prompt(
                    text: "Key password: ",
                    secure_text: true
                   )

#   Release the free build to GitHub because billing only works if signed by Google Play
    gradle(task: "assembleRelease")

    Dir.chdir("..") do
      gradle(task: "app:bundleRelease")
    end

    apk_path_release="app/build/outputs/apk/release/keymapper-" + version_name + "-foss.apk"

    # First release to google play so any errors with the descriptions are resolved before
    # creating the git tag.
    supply(
        aab: "../app/build/outputs/bundle/release/app-release.aab",
        track: "internal",
        skip_upload_apk: true
    )

    whats_new = File.read("../base/src/main/assets/whats-new.txt")

    github_release = set_github_release(
      repository_name: "keymapperorg/KeyMapper",
      api_bearer: github_token,
      name: version_name,
      tag_name: "v" + version_name,
      description: whats_new,
      commitish: "master",
      upload_assets: [apk_path_release],
      is_draft: false,
      is_prerelease: false
    )
end

desc "Create and deploy internal testing release"
lane :internal do
    version_code = get_properties_value(key: "VERSION_CODE", path: "./app/version.properties")
    version_name = get_properties_value(key: "VERSION_NAME", path: "./app/version.properties")

    # Check Rust formatting and run tests
    Dir.chdir("../evdev/src/main/rust/evdev_manager") do
      sh("cargo fmt --check")
      sh("cargo test --package evdev_manager_core")
    end

    Dir.chdir("..") do
      gradle(task: "ktlintFormat")
      gradle(task: "ktlintCheck")
      gradle(task: "testDebugUnitTest")
    end

#   Don't create changelog for f-droid because not committing it
#   File.write("metadata/android/en-US/changelogs/" + version_code + ".txt", whats_new)

    ENV["KEYSTORE_PASSWORD"] = prompt(
                    text: "Key store password: ",
                    secure_text: true
                   )

    ENV["KEY_PASSWORD"] = prompt(
                    text: "Key password: ",
                    secure_text: true
                   )

    Dir.chdir("..") do
      gradle(task: "app:bundleRelease")
    end

      # First release to google play so any errors with the descriptions are resolved before
      # creating the git tag.
      supply(
          aab: "../app/build/outputs/bundle/release/app-release.aab",
          track: "internal",
          skip_upload_apk: true,
          skip_upload_metadata: true,
          skip_upload_changelogs: true,
          skip_upload_images: true,
          skip_upload_screenshots: true
      )
end

